<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'ACTION_userGoodsServerUse'">添加卡权益 </a-button>
        <a-button type="primary" @click="handleAddNum(selectedRow)" :disabled="!hasSelectedRows || selectedRow.cardserverState == 0" v-auth="'ACTION_userGoodsServerUse'"> 权益增加 </a-button>
        <a-button type="primary" @click="handleDelay(selectedRow)" :disabled="!hasSelectedRows || selectedRow.cardserverState == 0" v-auth="'ACTION_userGoodsServerDelay'">权益延期 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <BusCardserverDrawer @register="registerDrawer" @success="handleSuccess" />
    <BusCardserverRecordModal @register="registerCardserverRecordModal" />
    <BusCardserverCommonUseDrawer @register="registerCommonUseDrawer" @success="handleSuccess" />
    <BusCardserverUseAppoinmentDrawer @register="registerUseAppoinmentDrawer" @success="handleSuccess" />
    <BusCardserverUseDelayriskDrawer @register="registerUseDelayriskDrawer" @success="handleSuccess" />
    <BusCardserverUseGuestDrawer @register="registerUseGuestDrawer" @success="handleSuccess" />
    <BusCardserverDelayDrawer @register="registerCardserverDelayDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="cardserver">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useModal } from '@apps/antd-admin/src/components/modal';
import BusCardserverDrawer from './components/BusCardserverDrawer.vue';
import BusCardserverDelayDrawer from './components/BusCardserverDelayDrawer.vue';
import BusCardserverRecordModal from './components/BusCardserverRecordModal.vue';
import BusCardserverCommonUseDrawer from './components/BusCardserverCommonUseDrawer.vue';
import BusCardserverUseAppoinmentDrawer from './components/BusCardserverUseAppoinmentDrawer.vue';
import BusCardserverUseDelayriskDrawer from './components/BusCardserverUseDelayriskDrawer.vue';
import BusCardserverUseGuestDrawer from './components/BusCardserverUseGuestDrawer.vue';
import { useBaseTable } from '@apps/antd-admin/src/hooks';

import { columns, searchFormSchema } from './data/cardserver.data';
import { Api } from './data/cardserver.api';
import { reactive } from 'vue';

const [registerDrawer, { openDrawer }] = useDrawer();
const [registerCardserverRecordModal, { openModal: openCardserverRecordModal }] = useModal();
const [registerCardserverDelayDrawer, { openDrawer: openCardserverDelayDrawer }] = useDrawer();
const [registerCommonUseDrawer, { openDrawer: openCommonUseDrawer }] = useDrawer();
const [registerUseAppoinmentDrawer, { openDrawer: openUseAppoinmentDrawer }] = useDrawer();
const [registerUseDelayriskDrawer, { openDrawer: openUseDelayriskDrawer }] = useDrawer();
const [registerUseGuestDrawer, { openDrawer: openUseGuestDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handleBatchDelete } = useBaseTable({
  designScope: 'cardserver-template',
  tableProps: {
    //表格标题
    title: '卡权益列表',
    //主键字段
    rowKey: 'cardserverId',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    //操作列配置
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
    useSearchForm: false,
  },
});

const [registerTable, { reload, setProps }, { rowSelection, selectedRow, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '普通使用',
      onClick: handleCommonOpt.bind(null, record),
      auth: 'ACTION_userGoodsServerUse',
      ifShow: () => {
        let cardserverState = record.cardserverState;
        return cardserverState == 1;
      },
    },
    {
      label: '特殊使用',
      onClick: handleSpecialOpt.bind(null, record),
      auth: 'ACTION_userGoodsServerUse',
      ifShow: () => {
        let serverCode = record.serverCode;
        let cardserverState = record.cardserverState;
        return (serverCode == 'delayrisk' || serverCode == 'transfer' || serverCode == 'vipServer') && cardserverState == 1;
      },
    },
    {
      label: '权益日志',
      onClick: handleLookCardServerRecord.bind(null, record),
      auth: 'ACTION_userGoodsServerUseLog',
    },
  ];
}

function handleCreate() {
  let record = { userGoodsId: searchInfo.userGoodsId };
  openDrawer(true, {
    record,
    isUpdate: false,
  });
}

/**
 * 编辑事件
 * @param record
 */
function handleEdit(record) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 公共使用
 * @param record
 */
function handleCommonOpt(record) {
  record['optType'] = 2;
  openCommonUseDrawer(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 特殊使用
 * @param record
 * @param optType
 */
function handleSpecialOpt(record) {
  record['optType'] = 2;
  let serverCode = record.serverCode;
  if (serverCode == 'delayrisk') {
    openUseDelayriskDrawer(true, {
      record,
      isUpdate: true,
    });
  } else if (serverCode == 'transfer') {
    openUseGuestDrawer(true, {
      record,
      isUpdate: true,
    });
  } else if (serverCode == 'vipServer') {
    openUseAppoinmentDrawer(true, {
      record,
      isUpdate: true,
    });
  }
}

function handleAddNum(record) {
  record['optType'] = 1;
  openCommonUseDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleDelay(record) {
  openCardserverDelayDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleLookCardServerRecord(record) {
  openCardserverRecordModal(true, {
    record,
    isUpdate: false,
  });
}

const searchInfo = reactive<Recordable>({});

function setSearchInfoProps(userGoodsId) {
  console.info('userGoodsId', userGoodsId);
  searchInfo.userGoodsId = userGoodsId;
}

defineExpose({
  setSearchInfoProps,
});
</script>
